package com.sample.transaction.atomikos.combine.service;

import cn.hutool.core.util.RandomUtil;
import com.sample.transaction.atomikos.db1.entity.User1;
import com.sample.transaction.atomikos.db1.service.User1Service;
import com.sample.transaction.atomikos.db2.entity.User2;
import com.sample.transaction.atomikos.db2.service.User2Service;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class CombineServiceImpl implements CombineService {

    private final User1Service user1Service;

    private final User2Service user2Service;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void testStep(Boolean step1, Boolean step2, Boolean throwEx) {
        if(Boolean.TRUE.equals(step1)){
            User1 user = new User1();
            user.setCode(RandomUtil.randomString(10));
            user.setName(RandomUtil.randomString(10));
            user1Service.updateUser(user, Boolean.FALSE);
        }
        if(Boolean.TRUE.equals(step2)){
            User2 user = new User2();
            user.setCode(RandomUtil.randomString(10));
            user.setName(RandomUtil.randomString(10));
            user2Service.updateUser(user, throwEx);
        }
    }

}
